package com.anbinh.doctor.fragment.clinic;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.MessagesAdapter;
import com.anbinh.doctor.fragment.tinnhan.FragmentMessages;
import com.anbinh.doctor.model.Notification;
import com.anbinh.doctor.model.NotificationList;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;

public class FragmentClinicMessagesResultSearch extends Fragment {
	private static FragmentClinicMessagesResultSearch fragment = null;
	private View mView;
	private UserLogin userLogin;
	protected MessagesAdapter adapter;
	protected ArrayList<Notification> mList;
	protected ListView mGridView;
	protected Notification currentNotification;
	private static String mKeyName;

	public static FragmentClinicMessagesResultSearch newInstance(String keyName) {
		if (fragment == null) {
			fragment = new FragmentClinicMessagesResultSearch();
		}
		mKeyName = keyName;
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				FragmentClinicMessages.newInstance().currentNotification = mList
						.get(position);
				mList.get(position).setStatus(1);
				adapter.notifyDataSetChanged();
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, R.id.message_detailt);
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.chi_tiet_tin_nhan));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});
		adapter = null;
		onLoadData();
	}

	public void onLoadData() {
		Utils.onLoadding(mView, true);
		ServerController.getInstance(getActivity()).requestNotificationSearch(
				userLogin.getData().getToken(), mKeyName,
				new OnRequest<NotificationList>() {

					@Override
					public void onSuccess(NotificationList data) {
						Utils.onLoadding(mView, false);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() < 0)
							return;

						if (adapter == null) {
							mList = data.getData().getNotificationsList();
							adapter = new MessagesAdapter(getActivity(),
									R.layout.item_message, mList);
							mGridView.setAdapter(adapter);
						} else {
							mList.addAll(data.getData().getNotificationsList());
							adapter.notifyDataSetChanged();
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.onLoadding(mView, false);

					}
				});
	}

}
